package com.ricky.spring.demo.controller;

import com.google.common.collect.Maps;
import com.ricky.spring.demo.config.WxKefuConfProperties;
import com.ricky.spring.demo.model.WxKefuConf;
import com.ricky.spring.demo.service.AnimalHello;
import com.ricky.spring.demo.service.DemoConfig;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * @author leiruiqi {@literal <leiruiqi@baijiahulian.com>}
 **/
@Import(WxKefuConfProperties.class)
@Slf4j
@RefreshScope
@RequestMapping("test")
@RestController
public class TestController {

    @Resource
    private DemoConfig demoConfig;

    @Resource
    private AnimalHello catHello;

    //@Value("${test.b}")
    //private String testb;

    @Value("${test.a}")
    private String testa;

    @Value("${test.random}")
    private Long testRandom;

    @Value("${final.result}")
    private String finalResult;

    @Value("${test.url.a}")
    private String urla;

    @Qualifier("dogHello")
    @Autowired
    private AnimalHello dogHello;


    private Map<String, WxKefuConf> wxappKefuConfMap;

    @Autowired
    private WxKefuConfProperties wxKefuConfProperties;

    @PostConstruct
    public void init(){
        Map<String, WxKefuConf> map = Maps.newHashMap();
        for(WxKefuConf wxKefuConf: wxKefuConfProperties.getConf()) {
            map.put(wxKefuConf.getAppId(),wxKefuConf);
        }

        this.wxappKefuConfMap = map;
        log.info("refresh wxappKefuConfMap={}",wxappKefuConfMap);
    }

    @RequestMapping("index")
    public String index(){
        return "ok";
    }

    @RequestMapping("checkConfHello")
    public String checkConfHello(){
        return demoConfig.sayHello();
    }

    @RequestMapping("dogHello")
    public String dogHello(){
        return dogHello.hello();
    }

    @RequestMapping("catHello")
    public String catHello(){
        return catHello.hello();
    }

    @RequestMapping("showAllConf")
    public Map<String,Object> showAllConf(){
        Map<String,Object> data = new HashMap<>();
        data.put("testa",testa);
       // data.put("testb",testb);
        data.put("testRandom",testRandom);
        data.put("finalResult",finalResult);
        data.put("urla",urla);
        return data;
    }

    @RequestMapping("urla")
    public String testurl(){
        return urla;
    }

    @RequestMapping("checkWxConf")
    public Object checkWxConf(String unionId){
        return wxappKefuConfMap;
    }
}
